#include "TextItemDelegate.hpp"
#include <QTextEdit>

using namespace ui;

TextItemDelegate::TextItemDelegate(QWidget *parent) :
    QItemDelegate(parent) { }

QWidget *TextItemDelegate::createEditor(
        QWidget *parent, const QStyleOptionViewItem &/* option */,
        const QModelIndex &/* index */) const {
    QTextEdit *textEdit = new QTextEdit(parent);
    textEdit->setReadOnly(true);
    textEdit->installEventFilter(const_cast<TextItemDelegate *>(this));
    return textEdit;
}

void TextItemDelegate::setEditorData(
        QWidget *editor, const QModelIndex &index) const {
    QString string = index.model()->data(index).toString();
    QTextEdit *textEdit = static_cast<QTextEdit *>(editor);
    textEdit->append(string);
    QTextCursor cursor(textEdit->textCursor());
    cursor.movePosition(QTextCursor::Start);
    textEdit->setTextCursor(cursor);
}

void TextItemDelegate::setModelData(
        QWidget * /* editor */, QAbstractItemModel * /* model */,
        const QModelIndex &/* index */) const {
    // This delegate is a read only property.
}

void TextItemDelegate::updateEditorGeometry(
        QWidget *editor, const QStyleOptionViewItem &option,
        const QModelIndex &/* index */) const {
    editor->setGeometry(option.rect);
}
